//
//  SignViewController.m
//  掌上海印
//
//  Created by admin on 16/4/13.
//  Copyright © 2016年 handsome. All rights reserved.
//

#import "SignViewController.h"
#import "SYQRCodeViewController.h"
#import "SignDetailViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "SignAllViewController.h"
#import "SignNotifViewController.h"
#import "AbsenceViewController.h"
static SystemSoundID shake_sound_male_id = 0;
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@implementation SignViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
     self.tableView.backgroundColor = bgColor;
    [self.view addSubview:self.tableView];
 self.navigationItem.leftBarButtonItem = nil;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else {
        return 3;

    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = [UIColor clearColor];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    [cell setSelected:NO animated:NO];
    if (indexPath.section == 0) {
        [cell.imageView setImage:[UIImage imageNamed:@"discover-scan"]];
        [cell.textLabel setText:@"扫一扫"];
     
    } else {
        if (indexPath.row == 0) {
            
            [cell.imageView setImage:[UIImage imageNamed:@"toolbar-text"]];
            [cell.textLabel setText:@"请假"];
        }else if (indexPath.row == 1) {
            [cell.imageView setImage:[UIImage imageNamed:@"tab_topic"]];
            [cell.textLabel setText:@"考勤表扬名单"];

        } else {
            
            [cell.imageView setImage:[UIImage imageNamed:@"toolbar-report"]];
            [cell.textLabel setText:@"考勤须知"];

        }
    }
       return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == 0) {
        [self sign];
    } else {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Sign" bundle:nil];
        if (indexPath.row == 0) {
            AbsenceViewController *vc = [story instantiateViewControllerWithIdentifier:@"AbsenceViewController"];
            [self.navigationController pushViewController:vc animated:YES];

        }else if (indexPath.row == 1){
            SignAllViewController *vc = [story instantiateViewControllerWithIdentifier:@"SignAllViewController"];
            [self.navigationController pushViewController:vc animated:YES];


        } else {
            
            SignNotifViewController *vc = [story instantiateViewControllerWithIdentifier:@"SignNotifViewController"];
            [self.navigationController pushViewController:vc animated:YES];

        }
    }
}

- (void)sign {
    //扫描二维码
    SYQRCodeViewController *qrcodevc = [[SYQRCodeViewController alloc] init];
    qrcodevc.SYQRCodeSuncessBlock = ^(SYQRCodeViewController *aqrvc,NSString *qrString){
        dispatch_async(dispatch_get_main_queue(), ^{
            [self playSound];
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Sign" bundle:nil];
            SignDetailViewController *vc = [story instantiateViewControllerWithIdentifier:@"SignDetailViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        });
        //        self.saomiaoLabel.text = qrString;
        [aqrvc dismissViewControllerAnimated:NO completion:nil];
    };
    qrcodevc.SYQRCodeFailBlock = ^(SYQRCodeViewController *aqrvc){
        //        self.saomiaoLabel.text = @"fail~";
        [aqrvc dismissViewControllerAnimated:NO completion:nil];
    };
    qrcodevc.SYQRCodeCancleBlock = ^(SYQRCodeViewController *aqrvc){
        [aqrvc dismissViewControllerAnimated:NO completion:nil];
        //        self.saomiaoLabel.text = @"cancle~";
    };
    [self.navigationController pushViewController:qrcodevc animated:YES];

}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)playSound {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
    if (path) {
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &shake_sound_male_id);
        AudioServicesPlaySystemSound(shake_sound_male_id);
    }
    
    AudioServicesPlaySystemSound(shake_sound_male_id);
}

@end
